package com.example.test_app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;

public class Layout extends FrameLayout {


    public Layout(Context context) {
        super(context);
    }

    public Layout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Layout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public Layout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    public void draw(Canvas canvas) {

       super.draw(canvas);
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = getChildCount();
        TreeSet<View> treeSet=new TreeSet<>(new Comparator<View>() {
            @Override
            public int compare(View o1, View o2) {
               if (o1==o2){
                   return 0;
               }else if (o1.getZ()<=o2.getZ()){
                   return -1;
               }else {
                   return 1;
               }
            }
        });
        for (int i = 0; i <count; i++) {
            treeSet.add(getChildAt(i));
        }

        Iterator<View> iterator=treeSet.iterator();
        while (iterator.hasNext()){
            drawChild(canvas,iterator.next(),0) ;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

    }


}
